﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Shop.Model;
using System.Web;
using Shop.Framework.Keys;

namespace Shop.Framework.Services {
    public class CMS_CategoryService : BaseService {
        public void AddCategory(Category cat) {
            try {

                cat.DateEncoded = DateTime.Now;
                ShopDatabaseDataContext context = Context;
                context.Categories.InsertOnSubmit(cat);
                context.SubmitChanges();

                List<Category> catList = HttpRuntime.Cache[CacheKey.CategoryList] as List<Category>;
                catList.Add(cat);
            } catch (Exception ex) {
                throw new Exception("Error on cms add category.", ex);
            }
        }

        public void EditCategory(Category cat) {
            try {


                ShopDatabaseDataContext context = Context;
                var match = context.Categories.FirstOrDefault(c => c.IdCategory == cat.IdCategory);
                match.Name = cat.Name;
                match.Description = cat.Description;
                match.Tags = cat.Tags;
                match.Keywords = cat.Keywords;
                match.IsDisabled = cat.IsDisabled;                
                context.SubmitChanges();

                List<Category> catList = HttpRuntime.Cache[CacheKey.CategoryList] as List<Category>;
                int index = catList.FindIndex(c => c.IdCategory == match.IdCategory);

                catList[index] = match;
            } catch (Exception ex) {
                throw new Exception("Error on cms add edit category.", ex);
            }
        }
    }
}
